'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['html', 'js', 'css']
const TerserPlugin = require('terser-webpack-plugin')
// process.env.NODE_ENV = 'production'
console.log(process.env.NODE_ENV)
function resolve(dir) {
    return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'vue Element Admin' // page title

const port = 8080

module.exports = {
    publicPath: './',
    outputDir: 'dist',
    assetsDir: 'static',
    lintOnSave: process.env.NODE_ENV === 'development',
    parallel: require('os').cpus().length > 1,
    productionSourceMap: false,
    devServer: {
        port: port,
        open: false,
        overlay: {
            warnings: false,
            errors: true
        },

        proxy: {
            // change xxx-api/login => mock/login
            // detail: https://cli.vuejs.org/config/#devserver-proxy
            [process.env.VUE_APP_BASE_API]: {
                // target: 'http://192.168.3.144:8088/',  // tdd本地
                // target: 'http://192.168.2.93:8088/',  // 93
                target: 'http://192.168.2.93:8090/',  // 内网
                // target: 'http://cdcxj.kmdns.net:8083/',  // 外网
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.VUE_APP_BASE_API]: ''
                }
            },
            "/": {
                // target: 'http://192.168.2.93:8088/',  // 内网
                target: 'http://192.168.2.93:8090/',  // 内网
                // target: 'http://cdcxj.kmdns.net:8083/',  // 外网
                changeOrigin: true,
                ws: false
            },
            "/api": {
                // target: 'http://192.168.2.93:8088/',  // 内网
                target: 'http://192.168.2.93:8090/',  // 内网
                // target: 'http://cdcxj.kmdns.net:8083/',  // 外网
                changeOrigin: true,
                ws: false
            },
        }

        // before: require('./mock/mock-server.js')
    },
    configureWebpack: {
        // provide the app's title in webpack's name field, so that
        // it can be accessed in index.html to inject the correct title.
        name: name,
        resolve: {
            alias: {
                '@': resolve('src')
            }
        },
        plugins: process.env.NODE_ENV !== 'production' ? [] : [
            new TerserPlugin({
                cache: true,
                parallel: true,
                sourceMap: false, // Must be set to true if using source-maps in production
                terserOptions: {
                    compress: {
                        drop_console: true,
                        drop_debugger: true
                    }
                }
            }),
            new CompressionWebpackPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: new RegExp(
                    '\\.(' + productionGzipExtensions.join('|') + ')$'
                ),
                threshold: 10240, // 只有大小大于该值的资源会被处理 10240
                minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
                deleteOriginalAssets: false // 删除原文件
            })
        ]
    },
    chainWebpack(config) {
        config.plugins.delete('preload') // TODO: need test
        config.plugins.delete('prefetch') // TODO: need test
        // set svg-sprite-loader
        config.module
            .rule('svg')
            .exclude.add(resolve('src/icons'))
            .end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]'
            })
            .end()

        // set preserveWhitespace
        config.module
            .rule('vue')
            .use('vue-loader')
            .loader('vue-loader')
            .tap(options => {
                options.compilerOptions.preserveWhitespace = true
                // options.compiler = require('styled-vue/compiler')
                return options
            })
            .end()
        config.module
            .rule('json')
            .test(/\.geojson$/)
            .use('json-loader')
            .loader('json-loader')
            .end()
        config.module
            .rule('json')
            .test(/\.geojson$/)
            .use('json-loader')
            .loader('json-loader')
            .end()
        config
            // https://webpack.js.org/configuration/devtool/#development
            .when(process.env.NODE_ENV === 'development',
                config => config.devtool('cheap-source-map')
            )
        config
            // https://webpack.js.org/configuration/devtool/#development
            .when(process.env.NODE_ENV !== 'development',
                config => config.devtool('none')
            )
        config
            .when(process.env.NODE_ENV === 'production',
                config => {
                    config
                        .plugin('ScriptExtHtmlWebpackPlugin')
                        .after('html')
                        .use('script-ext-html-webpack-plugin', [{
                            // `runtime` must same as runtimeChunk name. default is `runtime`
                            inline: /runtime\..*\.js$/
                        }])
                        .end()
                    config
                        .optimization.splitChunks({
                            chunks: 'all',
                            cacheGroups: {
                                libs: {
                                    name: 'chunk-libs',
                                    test: /[\\/]node_modules[\\/]/,
                                    priority: 10,
                                    chunks: 'initial', // only package third parties that are initially dependent
                                    reuseExistingChunk: true
                                },
                                elementUI: {
                                    name: 'chunk-elementUI', // split elementUI into a single package
                                    reuseExistingChunk: true,
                                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                                },
                                Echarts: {
                                    name: 'chunk-echart', // split elementUI into a single package
                                    reuseExistingChunk: true,
                                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                                    test: /[\\/]node_modules[\\/]_?echarts(.*)/ // in order to adapt to cnpm
                                },
                                Openlayers: {
                                    name: 'chunk-ol',
                                    reuseExistingChunk: true,
                                    test: /[\\/]node_modules[\\/]_?ol(.*)/, // can customize your rules
                                    priority: 20
                                }
                            }
                        })
                    config.optimization.runtimeChunk('single')
                }
            )
    }
}
